<template>
  <div class="app-container">

    <div ref="chart" style="width:80%;height:380px"></div>

    <el-table v-loading="loading" :data="productList" :row-class-name="tableRowClassName"
              @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" align="center" width="55" prop="id"/>
      <el-table-column label="商品名称" align="center" prop="name"/>
      <el-table-column label="商品类别" align="center" prop="categoryName"/>
      <el-table-column label="商品描述" align="center" prop="note"/>
      <!--      <el-table-column label="商品状态" align="center" prop="state"/>-->
      <el-table-column label="库存最小值" align="center" prop="minimum"/>
      <el-table-column label="库存最大值" align="center" prop="maximum"/>
      <el-table-column label="库存" align="center"  prop="balance"/>
      <!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
                >修改
                </el-button>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                >删除
                </el-button>

              </template>
            </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改仓库信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="108px">
        <el-form-item label="商品名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入商品名称"/>
        </el-form-item>
        <el-form-item label="商品类别" prop="categoryId">
          <el-select v-model="form.categoryId" placeholder="请输入商品类别">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品描述" prop="note">
          <el-input v-model="form.note" placeholder="请输入商品描述"/>
        </el-form-item>
        <!--        <el-form-item label="商品状态" prop="state">-->
        <!--          <el-input v-model="form.state" placeholder="请输入商品状态"/>-->
        <!--        </el-form-item>-->
        <el-form-item label="库存警戒值" prop="minimum">
          <el-input v-model="form.minimum" placeholder="请输入库存警戒值"/>
        </el-form-item>
        <el-form-item label="库存初始值" prop="balance">
          <el-input v-model="form.balance" placeholder="请输入库存初始值"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addProduct, delProduct, getProduct, listProduct, updateProduct } from '@/api/emsboot/product'
import { listCategory } from '@/api/emsboot/category'

export default {
  name: 'Product',
  components: {},
  data() {
    return {
      options: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 仓库信息表格数据
      productList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        categoryId: null,
        note: null,
        state: null,
        minimum: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        minimum: [
          { required: true, message: '库存警戒值不能为空', trigger: 'blur' }
        ],
        categoryId: [
          { required: true, message: '库存类别不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  mounted() {

  },
  methods: {
    getEchartData() {
      const chart = this.$refs.chart
      if (chart) {
        const myChart = this.$Mecharts.init(chart)
        const option = {
          title: {
            text: '库存一览图'
          },
          tooltip: {},
          legend: {
            data: ['库存']
          },
          xAxis: {
            data: this.productList.map(item => item.name)
          },
          yAxis: {},
          series: [
            {
              name: '库存',
              type: 'bar',
              data: this.productList.map(item => item.balance),
              color: [
                '#7a225f',
              ]
            }
          ]
        }
        myChart.setOption(option)
        window.addEventListener('resize', function() {
          myChart.resize()
        })
      }
      this.$on('hook:destroyed', () => {
        window.removeEventListener('resize', function() {
          myChart.resize()
        })
      })
    }
    ,
    /** 查询仓库信息列表 */
    getList() {
      this.loading = true
      listCategory().then(response => {
        this.options = response.rows

      })
      listProduct(this.queryParams).then(response => {
        this.productList = response.rows

        const res = this.productList.map(item => item.name)
        this.getEchartData()
        console.log(res,"res")
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        categoryId: null,
        note: null,
        state: null,
        minimum: null,
        balance: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加商品信息'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getProduct(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改商品信息'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProduct(this.form).then(response => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addProduct(this.form).then(response => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除编号为"' + ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delProduct(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      })
    },
    tableRowClassName({ row, rowIndex }) {
      if (row.balance < row.minimum) {
        return 'success-row'
      }
      return ''
    }
  }
}
</script>
<style>
.el-table .success-row {
  background: #ce89ba;
}
</style>
